/*
 * (C) Copyright 2005-2010 by Burrp Software Pvt. Ltd. All Rights Reserved.
 *
 * This software is the proprietary information of Burrp Software Pvt. Ltd.
 * Use is subject to license terms.
 *
 * Burrp Software Pvt. Ltd. -- http://www.burrp.com
 */
package com.showmemysite;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.gargoylesoftware.htmlunit.html.HTMLParser;
import com.showmemysite.register.BroadCast;
import com.showmemysite.rules.*;

public class Program extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Program() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		String url = (String)request.getParameter("link");
		PrintWriter out = response.getWriter();
		String contents = processHTMLPage(getURL(url), url);
		out.println(contents);
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}
	
	private String getURL(String address){
		StringBuilder html = new StringBuilder();
		URLConnection  uCon = null;
		InputStream is = null;
		try {
			URL url;
			url= new URL(address);
			uCon = url.openConnection();
			uCon.setReadTimeout(10);
			is = uCon.getInputStream();
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				html.append(inputLine);
			}
			in.close();
		}
		catch (Exception e) {
			return "";
		}
		return html.toString();
	}
	
	private String processHTMLPage(String content, String baseUrl){
		String processedHTML = content;
		int indexHead = processedHTML.indexOf("<head>");
		if(indexHead>0){
			processedHTML = processedHTML.substring(0,indexHead)+"<base href=\""+baseUrl+"\" />"+processedHTML.substring(indexHead);
		}
		processedHTML = StripLinks.stripLinks(processedHTML, baseUrl);
		processedHTML = StripBadWords.strip(processedHTML);
		String finalHTML = Header.addHeader(processedHTML, baseUrl);
		BroadCast.version++;
		BroadCast.html = finalHTML;
	  return finalHTML;
	}
	


	

}
